package com.qf.openapi.web.master.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.DigestUtil;
import com.qf.openapi.web.master.dao.UserDao;
import com.qf.openapi.web.master.entity.User;
import com.qf.openapi.web.master.service.UserService;
import com.qf.openapi.web.master.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User getUser(Integer id) {
        return userDao.getUserById(id);
    }

//增删改
/*    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateUserStatus(User user) {

        userDao.updateUser(user);

        int a = 1/0;

    }*/
    //查询
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User login(String username, String password) {
        //1.根据用户名查询用户信息

       User user= userDao.getUserByName(username);
        if (user==null){ //没用匹配的用户直接返回null
            return null;
        }else {
            //比对密码是否正确
            String password1 = user.getPassword();
            //转换 password 为 md5字符串（需要hutool工具类）
            //讲暗问的密码转换成明文密码
            String md5password = DigestUtil.md5Hex(password);
            if (password1.equals(md5password)){
                return user;
            }
            return null;
        }
    }



    //增删改
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean regist(User user) {

        Integer count = userDao.insertUsers(user);
        if (count!=0){
            return true;
        }else {
            return false;
        }
    }




    }

